package io.task.cc.network.spi.server;

import io.task.cc.network.ChannelContext;
import io.task.cc.network.Config;
import io.task.cc.network.Handler;
import io.task.cc.network.utils.thread.pool.SynThreadPoolExecutor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@Setter
@Getter

public class ServerConfig extends Config {

    private AcceptCompletionHandler acceptCompletionHandler = null;

    private Thread checkHeartbeatThread = null;
    private boolean needCheckHeartbeat = true;

    private boolean isShared = false;


    public ServerConfig(String name, SynThreadPoolExecutor tioExecutor,
                        ThreadPoolExecutor groupExecutor) {
        super(tioExecutor, groupExecutor);


        init(name);
    }


    private void init(String name) {
        this.name = name;
        this.groupStat = new ServerGroupStat();
        this.acceptCompletionHandler = new AcceptCompletionHandler();

        checkHeartbeatThread = new Thread(new Runnable() {
            @Override
            public void run() {
                //第一次先休息一下
                try {
                    Thread.sleep(1000 * 10);
                } catch (InterruptedException e1) {
                    log.error(e1.toString(), e1);
                }

                while (needCheckHeartbeat && !isStopped()) {
                    //					long sleeptime = heartbeatTimeout;
                    if (heartbeatTimeout <= 0) {
                        log.info("{}, 用户取消了框架层面的心跳检测，如果业务需要，请用户自己去完成心跳检测", ServerConfig.this.name);
                        break;
                    }
                    try {
                        Thread.sleep(heartbeatTimeout);
                    } catch (InterruptedException e1) {
                        log.error(e1.toString(), e1);
                    }


                    Set<ChannelContext> set = null;

                    ReentrantLock readLock = new ReentrantLock();
                    readLock.lock();
                    try {


                        for (ChannelContext channelContext : set) {

                            long compareTime = Math.max(channelContext.channelStat.latestTimeOfReceivedByte, channelContext.channelStat.latestTimeOfSentPacket);
                            long currtime = System.currentTimeMillis();
                            long interval = currtime - compareTime;

                            boolean needRemove = false;
                            if (channelContext.heartbeatTimeout != null && channelContext.heartbeatTimeout > 0) {
                                needRemove = interval > channelContext.heartbeatTimeout;
                            } else {
                                needRemove = interval > heartbeatTimeout;
                            }

                            if (needRemove) {

                            }
                        }
                    } catch (Throwable e) {
                        log.error("", e);
                    } finally {
                        try {
                            readLock.unlock();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }


            }
        }, "tio-timer-checkheartbeat-" + id + "-" + name);
        checkHeartbeatThread.setDaemon(true);
        checkHeartbeatThread.setPriority(Thread.MIN_PRIORITY);
        checkHeartbeatThread.start();
    }


    @Override
    public Handler getTioHandler() {
        return null;
    }


    @Override
    public boolean isServer() {
        return true;
    }


}
